    //
//  ItemListController.m
//  123
//
//  Created by Wang Jun on 11-8-7.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ItemListController.h"
#import "ItemDetailController.h"
#import "SearchListCell.h"

@implementation ItemListController

@synthesize search,searchTableView;
@synthesize searchList;


// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

- (void)goHomePage{
	
	ItemDetailController *itemDetailController = [[ItemDetailController alloc] init];
	[self.navigationController pushViewController:itemDetailController animated:YES];
	[itemDetailController release];
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[self.navigationController setNavigationBarHidden:NO animated:YES];
	
	[self.navigationItem setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithTitle:@"HOME" 
				style:UIBarButtonItemStylePlain  target:self action:@selector(goHomePage)] autorelease] animated:YES];
	
	search.delegate = self;
	
	NSMutableArray *_list = [[NSMutableArray alloc] initWithObjects:@"1",@"2",nil];
	self.searchList = _list;
	[_list release];
	
    [super viewDidLoad];
	
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}


- (void)viewDidAppear:(BOOL)animated {
	
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
	
	[super viewDidDisappear:animated];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
	[super viewDidUnload];
	
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[search release];
	[searchTableView release];
    [super dealloc];
}


#pragma mark -
#pragma mark Table Data Source Methods

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
	
	return 1;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
	
	
	return @"";
	
}

-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
	return [self.searchList count] + 1;
	
}

-(UITableViewCell *)tableView:(UITableView *)tableView
		cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	
	NSUInteger row = [indexPath row];
	static NSString *ItemListCellIndentifier = @"ItemListCellIndentifier";
	static NSString *LoadMoreCellIndentifier = @"LoadMoreCellIndentifier";
	
	
	if(row == ([self.searchList count])) {
		UITableViewCell *loadMoreCell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:
															LoadMoreCellIndentifier];
		if (loadMoreCell == nil) {
			loadMoreCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
												   reuseIdentifier:LoadMoreCellIndentifier] autorelease];
			loadMoreCell.textLabel.text = @"查看更多"; 
			loadMoreCell.textLabel.textAlignment=UITextAlignmentCenter;
			loadMoreCell.textLabel.font = [UIFont fontWithName:@"Arial" size:14];
			
		}
		return loadMoreCell;
		
	}
	
	SearchListCell *cell = (SearchListCell *)[tableView dequeueReusableCellWithIdentifier:
											  ItemListCellIndentifier];
	if(cell == nil){
		
		NSArray *nibs = [[NSBundle mainBundle]
						 loadNibNamed:@"SearchListCell" 
						 owner:self 
						 options:nil];
		for (id one in nibs) {
			if ([one isKindOfClass:[SearchListCell class]]) {
				cell =  (SearchListCell *)one;
			}
			
		}
		
	}
	
	[self.searchList objectAtIndex:row];
	
	cell.custImageView.image = [UIImage imageNamed:@"image.png"];
	cell.titlesLabel.text = @"<张家界-凤凰-袁家寨子双飞5日>";
	cell.priceLabel.text = @"价格:2880起";
	cell.rateLabel.text = @"满意度:92%";
	cell.dayLabel.text = @"行程:5天";
	cell.typeLabel.text = @"工具:飞机";
	
	return cell;
	
}

#pragma mark -
#pragma mark Table Delegate methods



-(void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	
	//NSUInteger section = [indexPath section];
//	NSUInteger row = [indexPath row];
//	[self.searchTableView deselectRowAtIndexPath:indexPath animated:YES];
	
}


-(CGFloat) tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	return kTableViewRowHeight;
}

#pragma mark -
#pragma mark search bar Delegate methods

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
	[searchBar setShowsCancelButton:NO animated:YES];
	NSLog(@"input :%@",[searchBar text]);
	NSLog(@"searching");
}

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar{
	[searchBar setShowsCancelButton:NO animated:YES];
    [searchBar resignFirstResponder];
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    NSLog(@"BeginEditing");  
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
	NSLog(@"textDidChange");  
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
	[searchBar setShowsCancelButton:YES animated:YES];
	NSLog(@"searchBarShouldBeginEditing"); 
	return YES;
}

@end
